package com.example.security.mapper;

import com.example.security.dto.UserQuery;
import com.example.security.entity.SysUser;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Optional;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM sys_user WHERE id = #{userId}")
    Optional<SysUser> findByUserId(Long userId);

    @Select("SELECT * FROM sys_user WHERE id = #{id}")
    SysUser selectById(Long id);

    @Select("SELECT username FROM sys_user WHERE id = #{id}")
    String selectUsernameById(Long id);

    @Insert("INSERT INTO sys_user(username, password, name, phone, email, dept_id, status, create_time) " +
            "VALUES(#{username}, #{password}, #{name}, #{phone}, #{email}, #{deptId}, #{status}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(SysUser user);

    @Update("UPDATE sys_user SET name=#{name}, phone=#{phone}, email=#{email}, " +
            "dept_id=#{deptId}, status=#{status}, update_time=#{updateTime} " +
            "WHERE id=#{id}")
    void update(SysUser user);

    @Delete("DELETE FROM sys_user WHERE id = #{id}")
    void delete(Long id);

    @Select("<script>" +
            "SELECT COUNT(*) FROM sys_user u " +
            "<where>" +
            "<if test='username != null and username != \"\"'>" +
            "  AND u.username LIKE CONCAT('%', #{username}, '%')" +
            "</if>" +
            "<if test='name != null and name != \"\"'>" +
            "  AND u.name LIKE CONCAT('%', #{name}, '%')" +
            "</if>" +
            "<if test='phone != null and phone != \"\"'>" +
            "  AND u.phone LIKE CONCAT('%', #{phone}, '%')" +
            "</if>" +
            "<if test='deptId != null'>" +
            "  AND u.dept_id = #{deptId}" +
            "</if>" +
            "<if test='status != null'>" +
            "  AND u.status = #{status}" +
            "</if>" +
            "<if test='deptIds != null and deptIds.size() > 0'>" +
            "  AND u.dept_id IN " +
            "  <foreach collection='deptIds' item='deptId' open='(' separator=',' close=')'>" +
            "    #{deptId}" +
            "  </foreach>" +
            "</if>" +
            "${dataScope}" +
            "</where>" +
            "</script>")
    long selectCount(UserQuery query);

    @Select("<script>" +
            "SELECT * FROM sys_user u " +
            "<where>" +
            "<if test='username != null and username != \"\"'>" +
            "  AND u.username LIKE CONCAT('%', #{username}, '%')" +
            "</if>" +
            "<if test='name != null and name != \"\"'>" +
            "  AND u.name LIKE CONCAT('%', #{name}, '%')" +
            "</if>" +
            "<if test='phone != null and phone != \"\"'>" +
            "  AND u.phone LIKE CONCAT('%', #{phone}, '%')" +
            "</if>" +
            "<if test='deptId != null'>" +
            "  AND u.dept_id = #{deptId}" +
            "</if>" +
            "<if test='status != null'>" +
            "  AND u.status = #{status}" +
            "</if>" +
            "<if test='deptIds != null and deptIds.size() > 0'>" +
            "  AND u.dept_id IN " +
            "  <foreach collection='deptIds' item='deptId' open='(' separator=',' close=')'>" +
            "    #{deptId}" +
            "  </foreach>" +
            "</if>" +
            "${dataScope}" +
            "</where>" +
            "ORDER BY u.create_time DESC " +
            "LIMIT #{pageSize} OFFSET #{offset}" +
            "</script>")
    List<SysUser> selectPage(UserQuery query);


    SysUser selectByUsername(String username);

    void updateOrgId(Long orgId, List<Long> userIds);

    void deleteById(Long userId);
}